<?php
namespace App\Services;
use Exception,Log;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Redis;
use App\Models\Linkslistsimages;

class BaiduService
{
    // 获取百度AccessToken
    public static function getBaiduAccessToken()
    {
        // https://ai.baidu.com/ai-doc/ANTIPORN/jk42xep4e
        $baidu_access_token = Redis::get("baidu_access_token");
        if (empty($baidu_access_token)) {
            $client_id = "BdomdVGwIUiM989SbyhPiU6H";
            $client_secret = "hmivv82vHsfwrNw1nDZPlUVG2Hj9jEz5";
            $client = new Client;
            $response = $client->request('GET', 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$client_id.'&client_secret='.$client_secret);
            $body = $response->getBody()->getContents();
            $responsebody = json_decode($body);
            $baidu_access_token = $responsebody->access_token;
            Redis::setex("baidu_access_token", 2505600, $baidu_access_token);
        }
        return ["status"=>"success","msg"=>"获取成功！","baidu_access_token"=>$baidu_access_token];
    }

    // 百度图片审核
    public static function baiduBce($baidu_access_token,$imgurl)
    {
        try {
            $requestUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $baidu_access_token;
            $client = new Client;
            $response = $client->request('POST', $requestUrl, [
                'form_params' => [
                    'imgUrl' => $imgurl
                ]
            ]);
            $body = $response->getBody()->getContents();
            $responsebody = json_decode($body);
            return $responsebody;
        } catch (Exception $e) {
            return json_decode('{"conclusionType":1}');
        }
    }

    // 百度文本内容审核
    public static function textSimpleCheck($text)
    {
        $textlists = ["习近平","胡锦涛","江泽民","毛泽东","彭丽媛","色情","博彩","彩票","菠菜","北京赛车","时时彩","幼女","嫖娼","毒品","冰毒","海洛因","摇头丸","笑气","内射","少妇","学生妹","白虎","淫乱","色图","黄图","后入","戴套","高潮","强奸","猥亵","偷拍","口爆","尾随","扣逼","群P","暴力","打架","杀人","抢劫","偷盗","诈骗","敲诈","勒索","绑架","鸦片","鸦片","尼玛","操你妈","狗逼","干你娘","傻逼","妈逼","群架"];
        $checkStatus = 1;
        foreach ($textlists as $textlist) {
            if(strpos($text,$textlist) !== false){
                $checkStatus = 2;
            }
        }
        return $checkStatus;
    }

    // 医疗行业关键词
    public static function textMedicalCheck($text)
    {
        $textlists = ["患者","康复","丰胸","痔疮","眼科","男科","阳痿","早泄","医院","疾病","病","顽疾","药","治疗","医生","医师","传染","传染","恢复健康","糖尿病","女性健康","男性健康","贷","千万","百万","十万","假一赔万","假一赔十","理财","投资","放款","官方认证","选股","免费领取","考研","官方客服","录取","变现","专治","中医","减肥","补肾"];
        $checkStatus = 1;
        $keyword = [];
        foreach ($textlists as $textlist) {
            if(strpos($text,$textlist) !== false){
                $checkStatus = 2;
                $keyword[] = $textlist;
            }
        }
        return ["code"=>$checkStatus,"keyword"=>implode(',', $keyword)];
    }

    // 百度文本内容审核
    public static function baiduTextCensor($baidu_access_token,$text)
    {
        try {
            $requestUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $baidu_access_token;
            $client = new Client;
            $response = $client->request('POST', $requestUrl, [
                'form_params' => [
                    'text' => $text
                ]
            ]);
            $body = $response->getBody()->getContents();
            $responsebody = json_decode($body);
            return $responsebody;
        } catch (Exception $e) {
            return json_decode('{"conclusionType":1}');
        }
    }

    // 百度内容审核
    public static function baiduCheck($users_id,$imgurl)
    {
        $baidu_access_token = self::getBaiduAccessToken();
        $baiduBce = self::baiduBce($baidu_access_token["baidu_access_token"],$imgurl);
        $checkStatus = 1;
        try {
            if ($baiduBce->conclusionType == 2) {
                // 可能存在多个违规结果
                $datas = $baiduBce->data;
                // 0是百度库,16为徽章
                $typeLists = [1,2,3,5,6,7,8,10,11,12,13,14,15,21,24];
                foreach ($datas as $data) {
                    // 1：色情识别、2：暴恐识别、5：政治敏感识别
                    if (in_array($data->type, [1,2,3,5])) {
                        $checkStatus = 3;
                    } elseif (in_array($data->type, $typeLists)) {
                        $checkStatus = 2;
                    }
                }
                // 添加违规照片
                Linkslistsimages::linksListsImagesInsert($users_id,$imgurl,$checkStatus,$baiduBce->data[0]->msg);
            } else {
                // 符合审核条件
            }
            return $checkStatus;
        } catch (Exception $e) {
            return $checkStatus;
        }
    }

}
